
package com.yimixian.app.util;

import com.ta.utdid2.android.utils.StringUtils;
import com.yimixian.app.SystemFramework;
import com.yimixian.app.address.ShortAddress;
import com.yimixian.app.cart.CartManager;
import com.yimixian.app.common.Constants;
import com.yimixian.app.common.SharePreferenceKeys;
import com.yimixian.app.model.Address;
import com.yimixian.app.model.AppConfig;
import com.yimixian.app.model.Store;
import com.yimixian.app.model.User;
import com.yimixian.app.user.UserRegistrationActivity;
import com.ymx.sdk.util.ArrayListAndStringUtil;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.util.Log;

import java.util.ArrayList;

//SharedPreferences中写入、读取对象操作 
public class SharedPreferencesHelper {

    private static SharedPreferences mySharedPreferences = SystemFramework
            .getInstance().getGlobalContext()
            .getSharedPreferences(Constants.SHARED_PREFS_NAME, Context.MODE_PRIVATE);

    public static String getString(String key) {
        return mySharedPreferences.getString(key, "");
    }

    public static String getString(String key, String defaultValue) {
        return mySharedPreferences.getString(key, defaultValue);
    }
    public static boolean commitString(String key, String value) {
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    public static long getLong(String key, long data) {
        return mySharedPreferences.getLong(key, data);
    }
    public static boolean commitLong(String key, long data) {
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        editor.putLong(key, data);
        return editor.commit();
    }

    public static boolean commitFloat(String key, float data) {
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        editor.putFloat(key, data);
        return editor.commit();
    }

    public static int getInt(String key) {
        return mySharedPreferences.getInt(key, 1);
    }

    public static int getInt(String key, int defaultValue) {
        return mySharedPreferences.getInt(key, defaultValue);
    }
    public static float getFloat(String key, int defaultValue) {
        return mySharedPreferences.getFloat(key, defaultValue);
    }

    public static boolean clearFloat(String userKey){
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        editor.remove(userKey);
        return editor.commit();
    }

    public static boolean commitInt(String key, int value) {
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    public static boolean getBoolean(String key) {
        return mySharedPreferences.getBoolean(key, false);
    }

    public static boolean getBoolean(String key, boolean defaultValue) {
        return mySharedPreferences.getBoolean(key, defaultValue);
    }

    public static boolean commitBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }

    public static void clearData() {
        mySharedPreferences.edit().clear().commit();
    }

    public static void setFirstUseState(boolean state) {
        commitBoolean(Constants.APP_FIRST_USE_KEY, state);
    }

    public static boolean getFirstUseState() {
        return getBoolean(Constants.APP_FIRST_USE_KEY, true);
    }

    public static int getVersionCode() {
        return getInt(Constants.APP_VERSION_CODE, 0);
    }

    public static void setVersionCode(int versionCode) {
        commitInt(Constants.APP_VERSION_CODE, versionCode);
    }

    public static void setPickUpStoreName(String storeName) {
        commitString(Constants.PICK_UP_STORE_NAME, storeName);
    }

    public static String getPickUpStoreName() {
        return getString(Constants.PICK_UP_STORE_NAME);
    }

    public static void setIsLodingData(boolean isLodingData) {
        commitBoolean(Constants.IS_LOADING_DATA, isLodingData);
    }

    public static void saveAddresses(ArrayList<Address> address, String addressKey) {
        String content = new ArrayListAndStringUtil<Address>().ArrayListToString(address);
        commitString(addressKey, content);
    }

    private static void saveUser(ArrayList<User> users, String userKey) {
        String content = new ArrayListAndStringUtil<User>().ArrayListToString(users);
        commitString(userKey, content);
    }

    public static void saveStore(ArrayList<Store> stores, String storeKey) {
        String content = new ArrayListAndStringUtil<Store>().ArrayListToString(stores);
        commitString(storeKey, content);
    }
    public static void saveAddress(Address address, String addressKey) {
        ArrayList<Address> addresses = new ArrayList<>();
        addresses.add(address);
        saveAddresses(addresses, addressKey);
    }

    public static ArrayList<Address> getAddresses(String addressKey) {
        String address = getString(addressKey, "");
        return new ArrayListAndStringUtil<Address>().StringToArrayList(address);
    }

    public static ArrayList<ShortAddress> getShortAddresses(String addressKey) {
        String address = getString(addressKey, "");
        return new ArrayListAndStringUtil<ShortAddress>().StringToArrayList(address);
    }

    public static ArrayList<User> getUsers(String userKey) {
        String users = getString(userKey, "");
        return new ArrayListAndStringUtil<User>().StringToArrayList(users);
    }

    public static Address getAddress(String addressKey) {
        if (StringUtils.isEmpty(addressKey))
            return null;
        ArrayList<Address> addresses = getAddresses(addressKey);
        if (addresses == null || addresses.size() == 0) {
            return null;
        } else {
            return addresses.get(0);
        }
    }

    public static void clearAddress(String addressKey) {
        commitString(addressKey, "");
    }

    public static void saveCartViewLocation(Point point) {
        if (point == null) {
            return;
        }
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        editor.putInt(SharePreferenceKeys.CART_VIEW_X, point.x).commit();
        editor.putInt(SharePreferenceKeys.CART_VIEW_Y, point.y).commit();
    }

    public static Point getCartViewLocation() {
        int x = mySharedPreferences.getInt(SharePreferenceKeys.CART_VIEW_X, 0);
        int y = mySharedPreferences.getInt(SharePreferenceKeys.CART_VIEW_Y, 0);
        return new Point(x, y);
    }

    public static void setGuide2_1_0(boolean show) {
        commitBoolean(SharePreferenceKeys.SHOW_GUIDE_2_1_0, show);
    }

    public static boolean getGuide2_1_0() {
        return getBoolean(SharePreferenceKeys.SHOW_GUIDE_2_1_0, false);
    }
    public static void setGuideHome(boolean show) {
        commitBoolean(SharePreferenceKeys.SHOW_GUIDE, show);
    }

    public static boolean getGuideHome() {
        return getBoolean(SharePreferenceKeys.SHOW_GUIDE, false);
    }
    
    public static void setGuideSearch(boolean show) {
        commitBoolean(SharePreferenceKeys.SHOW_GUIDE_SEARCH, show);
    }

    public static boolean getGuideSearch() {
        return getBoolean(SharePreferenceKeys.SHOW_GUIDE_SEARCH, false);
    }
    
    public static void setGuideAddress(boolean show) {
        commitBoolean(SharePreferenceKeys.SHOW_GUIDE_ADDRESS, show);
    }

    public static boolean getGuideAddress() {
        return getBoolean(SharePreferenceKeys.SHOW_GUIDE_ADDRESS, false);
    }

    public static void setGuideComment(boolean show) {
        commitBoolean(SharePreferenceKeys.SHOW_GUIDE_COMMENT, show);
    }

    public static boolean getGuideComment() {
        return getBoolean(SharePreferenceKeys.SHOW_GUIDE_COMMENT, false);
    }

    /**
     * 保存商店
     * @param store
     * @param storeKey
     */
    public static void saveStore(Store store, String storeKey) {
        ArrayList<Store> stores = new ArrayList<>();
        CartManager.getInstance().saveStoreID(store.id);
        stores.add(store);
        saveStore(stores, storeKey);
    }

    private static ArrayList<Store> getStores(String storeKey) {
        String store = getString(storeKey, "");
        return new ArrayListAndStringUtil<Store>().StringToArrayList(store);
    }

    /**
     * 获取商店
     * @param storeKey
     * @return
     */
    public static Store getStore(String storeKey) {
        if (StringUtils.isEmpty(storeKey))
            return null;
        ArrayList<Store> stores = getStores(storeKey);
        if (stores == null || stores.size() == 0) {
            return null;
        } else {
            return stores.get(0);
        }
    }

    public static void saveRatingBarText1(String text) {
        commitString(SharePreferenceKeys.RATINGBAR_TEXT1,text);
    }

    public static void saveRatingBarText2(String text) {
        commitString(SharePreferenceKeys.RATINGBAR_TEXT2,text);
    }

    public static String getRatingBarText(String key) {
        return getString(key);
    }

    /**
     * 清除store
     * @param storeKey
     */
    public static void clearStore(String storeKey) {
        commitString(storeKey, "");
    }


    /**
     * 保存用户信息
     * @param user
     * @param userKey
     */
    public static void saveUser(User user, String userKey) {
        ArrayList<User> users = new ArrayList<>();
        users.add(user);
        saveUser(users, userKey);
    }

    /**
     * 获取用户信息
     * @param userKey
     * @return
     */
    public static User getUser(String userKey) {
        if (StringUtils.isEmpty(userKey))
            return null;
        if (userKey.equals(SharePreferenceKeys.CURRENT_USER)) {
            ArrayList<User> users = getUsers(userKey);
            if (users == null || users.size() == 0) {
                return null;
            } else {
                return users.get(0);
            }
        } else{
            return null;
        }
    }

    /**
     * 清除用户信息
     * @param userKey
     */
    public static void clearUser(String userKey) {
        commitString(userKey, "");
    }

    /**
     * 保存AppConfig
     * @param appConfig
     */
    public static void saveAppConfig(AppConfig appConfig) {
        ArrayList<AppConfig> appConfigs = new ArrayList<>();
        appConfigs.add(appConfig);
        saveAppConfig(appConfigs);
    }

    public static void saveAppConfig(ArrayList<AppConfig> appConfigs) {
        String content = new ArrayListAndStringUtil<AppConfig>().ArrayListToString(appConfigs);
        commitString(SharePreferenceKeys.YMX_APPCONFIG, content);
    }

    /**
     * 获取AppConfig
     * @return
     */
    public static AppConfig getAppConfig() {
        String appConfigsStr = getString(SharePreferenceKeys.YMX_APPCONFIG);
        ArrayList<AppConfig> appConfigs = new ArrayListAndStringUtil<AppConfig>().StringToArrayList(appConfigsStr);
        if(appConfigs == null || appConfigs.size() == 0) {
            return null;
        } else {
            if(appConfigs.get(0) == null) {
                return null;
            } else {
                AppConfig appConfig = appConfigs.get(0);
                return appConfig;
            }
        }
    }

    /**
     * 存储搜索历史
     * @param histories
     */
    public static void saveSearchHistory(ArrayList<String> histories) {
        String content = new ArrayListAndStringUtil<String>().ArrayListToString(histories);
        commitString(SharePreferenceKeys.SEARCH_HISTORY, content);
    }

    /**
     * 读取搜索历史
     * @return
     */
    public static ArrayList<String> getSearchHistory() {
        String histories = getString(SharePreferenceKeys.SEARCH_HISTORY, "");
        return new ArrayListAndStringUtil<String>().StringToArrayList(histories);
    }

    /**
     * 存储热门推荐
     * @param histories
     */
    public static void saveHotTips(ArrayList<String> histories) {
        String content = new ArrayListAndStringUtil<String>().ArrayListToString(histories);
        commitString(SharePreferenceKeys.HOT_TIPS, content);
    }

    /**
     * 读取热门推荐
     * @return
     */
    public static ArrayList<String> getHotTips() {
        String histories = getString(SharePreferenceKeys.HOT_TIPS, "");
        return new ArrayListAndStringUtil<String>().StringToArrayList(histories);
    }

    /**
     * 保存对象
     * @param userKey
     * @param obj
     */
    public static void saveObject(String userKey,Object obj) {
        commitString(userKey, IOUtil.objectToString(obj));
    }
    public static void clearObject(String userKey) {
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        editor.remove(userKey);
        editor.commit();
    }
    public static Object getObject(String userKey){
        if (StringUtils.isEmpty(userKey))
            return null;
        String str = getString(userKey);
        if(StringUtils.isEmpty(str)){
            return null;
        }
        return IOUtil.stringToObject(str);
    }
    public static boolean clearInt(String userKey){
        SharedPreferences.Editor editor = mySharedPreferences.edit();
        editor.remove(userKey);
        return editor.commit();
    }
}
